package com.lc.tgxm.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lc.tgxm.R;
import com.lc.tgxm.adapter.GvAdapter;
import com.lc.tgxm.model.BannerMod;
import com.lc.tgxm.model.PhotoMod;
import com.lc.tgxm.util.Log;
import com.lc.tgxm.widget.HomeBannerView;
import com.zcx.helper.glide.GlideLoader;
import com.zcx.helper.glide.transformations.CropCircleTransformation;
import com.zcx.helper.pager.Carousel;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.Timer;

/**
 * MyNormalizeViewActivity master alone
 * Created by Lei on 2017/1/11.
 */
public class MyNormalizeViewActivity extends BasePicActivity {
    private PopupWindow popWindow;
    private ArrayList<PhotoMod> picurlList;
    private Timer timer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initContenViewAndBack(R.layout.activity_normalize, R.string.normalize);

        HomeBannerView homeBannerView = (HomeBannerView) findViewById(R.id.hbv_home);
        homeBannerView.setOnCarouselItemListener(new Carousel.OnCarouselItemListener<BannerMod>() {
            @Override
            public void onCarouselItemClick(BannerMod bm) {
            }
        });
        homeBannerView.setItemList(BannerMod.testData());

        TextView textView = (TextView) findViewById(R.id.tv);
        Log.w(TAG, "textView:onCreate", textView);

        // 动态权限申请 地图定位相关权限
//        permissionLocationMap();

        ImageView iv1 = (ImageView) findViewById(R.id.iv_1);
        ImageView iv2 = (ImageView) findViewById(R.id.iv_2);
        ImageView iv3 = (ImageView) findViewById(R.id.iv_3);
        picurlList = new ArrayList<>();
        PhotoMod pm = new PhotoMod();
        pm.picurl = "http://uploadfile.bizhizu.cn/2015/0402/20150402113638741.jpg";
        PhotoMod pm2 = new PhotoMod();
        pm2.picurl = "http://pic23.nipic.com/20120915/10955561_103917634159_2.jpg";
        PhotoMod pm3 = new PhotoMod();
        pm3.picurl = "http://pic1.win4000.com/wallpaper/0/599b9158053d3.jpg";
        picurlList.add(pm);
        picurlList.add(pm2);
        picurlList.add(pm3);

        GlideLoader.getInstance().get(pm.picurl, iv1);
        GlideLoader.getInstance().get(pm2.picurl, iv2);
        GlideLoader.getInstance().get(pm3.picurl, iv3);

        initPopFromBottom();

        ArrayList<BannerMod> bannerModArrayList = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            BannerMod bm = new BannerMod();
            bm.title = "title";
            bm.resid = R.mipmap.ic_launcher;
            bannerModArrayList.add(bm);
        }

        GridView gridView = (GridView) findViewById(R.id.gv_home);
        GvAdapter gvAdapter = new GvAdapter(context, bannerModArrayList);
        gridView.setAdapter(gvAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UtilToast.show(position + "");
            }
        });
        textView.setText("SimpleDateFormatSimpleDateFormatSimpleDateFormatSimpleDateFormatSimpleDateFormatSimpleDateFormatSimpleDateFormat01234567980");

//        timer = new Timer();
//        timer.schedule(new TimerTask() {
//            @Override
//            public void run() {
//                runOnUiThread(new Runnable() {
//                    @Override
//                    public void run() {
//                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
//                        String ymd = sdf.format(System.currentTimeMillis());
//                        long dayoff = Utils.str2longTime(ymd + " 175000");
//                        long cha = dayoff - System.currentTimeMillis();
//                        textView.setText(Utils.ms2HourMinSecsColon(cha));
//                    }
//                });
//            }
//        }, 0, 1000);
    }

    // 初始化底部弹出popupwindow
    private void initPopFromBottom() {
        popWindow = initPopWindowFromBototm(R.layout.pw_selectbz);
        View pView = popWindow.getContentView();

        View takephoto = pView.findViewById(R.id.tv_takephoto);
        View pickpic = pView.findViewById(R.id.tv_pickpic);
        View cancel = pView.findViewById(R.id.tv_cancel);
        takephoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startCamera(null);
                popWindow.dismiss();
            }
        });
        pickpic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAlbum(null);
                popWindow.dismiss();
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });

    }

    @Override
    protected void onPermissionSuccess() {
//        UtilToast.show("Location Success");
//        TextView textView = (TextView) findViewById(R.id.tv);
//        Log.e(TAG, "textView", textView);
//        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
//        List<ScanResult> wifiList = wm.getScanResults();
//        Log.e(TAG, "wifiList.size()", wifiList.size());
//        for (int i = 0; i < wifiList.size(); i++) {
//            ScanResult result = wifiList.get(i);
//            textView.append("SSID:" + result.SSID + "\n");
//            textView.append("BSSID:" + result.BSSID + "\n");
//            Log.d("dada", "bssid=" + result.BSSID + " ssid:" + result.SSID);
//        }
    }

    @Override
    public void onButtonClick(View view) {
        switch (view.getId()) {
            case R.id.btn:
                showPwFromBottom(popWindow);
                break;
            case R.id.iv_1:
                Intent intent = new Intent(context, PhotoActivity.class);
                intent.putExtra("list", picurlList);
                intent.putExtra("position", 0);
                startActivity(intent);
                break;
            case R.id.iv_2:
                Intent intent2 = new Intent(context, PhotoActivity.class);
                intent2.putExtra("list", picurlList);
                intent2.putExtra("position", 1);
                startActivity(intent2);
                break;
            case R.id.iv_3:
                Intent intent3 = new Intent(context, PhotoActivity.class);
                intent3.putExtra("list", picurlList);
                intent3.putExtra("position", 2);
                startActivity(intent3);
                break;
        }
    }

    @Override
    protected void resultPhotoPath(ImageView imageView, String s) {
        ImageView ivTx = (ImageView) findViewById(R.id.iv_tx);
        GlideLoader.getInstance().get(s, ivTx, new CropCircleTransformation(context));

        // 走接口，上传头像
    }

    @Override
    protected void onDestroy() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        super.onDestroy();
    }
}